<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form inline :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" placeholder="分类名称"></el-input>
      </el-form-item>
<!--      <el-form-item label="店铺ID" prop="shopsId">-->
<!--        <el-select v-model="dataForm.shopsId" :disabled="disabled" clearable filterable placeholder="请选择"-->
<!--                   class="width185">-->
<!--          <el-option-->
<!--            v-for="shop in shops"-->
<!--            :key="shop.id"-->
<!--            :label="shop.name"-->
<!--            :value="shop.id">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status" :disabled="disabled">
          <el-radio :label="0">隐藏</el-radio>
          <el-radio :label="1">显示</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="分类名称" prop="level">
        <el-radio-group v-model="dataForm.level" :disabled="disabled">
          <el-radio :label="1">一级</el-radio>
          <el-radio :label="2">二级</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="父节点" v-if="dataForm.level!==1" prop="parentId">
        <el-select v-model="dataForm.parentId" :disabled="disabled" clearable filterable placeholder="请选择"
                   class="width185">
          <el-option
            v-for="parent in parents"
            :key="parent.id"
            :label="parent.name"
            :value="parent.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" :disabled="disabled" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        parents: [],
        dataForm: {
          id: '',
          name: '',
          shopsId: '',
          status: '',
          sort: '',
          level: 1,
          parentId: ''
        },
        dataRule: {
          name: [
            {
              required: true,
              message: '名称不能为空',
              trigger: 'blur'
            }
          ]
        },
        shops: []
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/shopscategory/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm = data.shopscategory
              }
            })
          }
          // this.$http({
          //   url: `/mall/shops/queryMyShop`,
          //   method: 'get'
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.shops = data.list
          //   }
          // })

          // 获取父级数据
          this.$http({
            url: '/mall/shopscategory/queryAll',
            method: 'get',
            params: {
              parentId: 0
            }
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.parents = data.list
            }
          })

        })
      },
      // 表单提交
      dataFormSubmit () {
        if(this.dataForm.level === 2 && this.dataForm.id === this.dataForm.parentId){
            alert("非法操作！ 不能引用自己！");
          return;
        }
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/mall/shopscategory/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({ data }) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
